package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsAdminMapper;
import com.qiguliuxing.dts.db.domain.DtsAdmin;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DtsAdminService {
	@Resource
	private DtsAdminMapper adminMapper;

	public List<DtsAdmin> findAdmin(String username) {
		QueryWrapper<DtsAdmin> entityWrapper = new QueryWrapper<>();
		entityWrapper.like("username", username);
		entityWrapper.eq("deleted", false);
		return adminMapper.selectList(entityWrapper);
	}

	public List<DtsAdmin> querySelective(String username, Integer page, Integer limit) {
		QueryWrapper<DtsAdmin> entityWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(username))
			entityWrapper.eq("username", username);
		entityWrapper.eq("deleted", false);
		entityWrapper.orderByDesc("update_time");
		Page<DtsAdmin> iPage = new Page<>(page, limit);
		iPage = adminMapper.selectPage(iPage, entityWrapper);
		log.info("查询结束，结果：{}", iPage);
		return iPage.getRecords();
	}

	public int updateById(DtsAdmin admin) {
		return adminMapper.updateById(admin);
	}

	public void deleteById(Integer id) {
		adminMapper.deleteById(id);
	}

	public void add(DtsAdmin admin) {
		admin.setAddTime(new Date());
		admin.setUpdateTime(new Date());
		adminMapper.insert(admin);
	}

	public DtsAdmin findById(Integer id) {
		return adminMapper.selectById(id);
	}
	public List<DtsAdmin> allAdmin() {
		return adminMapper.selectAll();
	}
}
